<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../static/lib/layui-v2.9.8/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../static/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <h2 align="center">产品出库</h2>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">产品名字</label>
        <div class="layui-input-block">
            <select name="fk_product" lay-search="" lay-verify="fk_product">
                <option value="">选择产品</option>
                {% for item in products %}
                    <option value="{{ item.pk }}">{{ item.name }}</option>
                {% endfor %}
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">出库数量</label>
        <div class="layui-input-block">
            <input type="number" name="quantity" lay-reqtext="数量不能为空" placeholder="请输入数量" value=""
                   class="layui-input" lay-precision="0" lay-affix="number" min="0">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../../static/lib/layui-v2.9.8/layui.js" charset="utf-8"></script>


<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        // ajax 头部增加csrf_token
        var token = "{{ csrf_token }}"
        $.ajaxSetup({
            headers: {
                'X-CSRFTOKEN': `${token}`,
                'AUTHORIZATION': 'Bearer ' + localStorage.token,
            }, // JWTAuthentication.authenticate()方法获取 header = request.META.get(api_settings.AUTH_HEADER_NAME),
        });

        $.ajaxSetup({
            complete: function (XMLHttpRequest, textStatus) {
                if (XMLHttpRequest.status === 401) {  // 401 表示未授权，即 token 无效
                    layer.msg('授权已过期，需重新登陆', {icon: 2, time: 2000}, function () {
                        window.location.href = '/login';
                    })
                }
            }
        });


        $(function () {   //页面加载时，AJAX请求
            $.ajax({
                url: '/auth-query',
                type: 'post',
                dataType: 'json',
                success: function (result) {
                    console.log(result);
                },
                error: function () {
                    layer.msg('授权查询API出错！');
                }
            })
        });

        form.verify({
            fk_product: function (value) {
                if (value.length === 0) {
                    return '请选择出库产品';
                }
            },
            quantity: function (value) {
                if (value.length === 0) {
                    return '请输入出库数量';
                }
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            //传给后台
            $.ajax({
                url: "/shipments_manage_send",
                type: "post",
                data: data.field,
                success: function (result) {
                    if (result.success) {
                        parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                        parent.layui.table.reload('currentTableId');
                        parent.parent_msg(result.msg)
                    } else {
                        layer.alert(result.msg);
                    }
                },
                error: function (result) {
                    alert("接口错误！！！");
                }
            });
            return false;
        });

    });
</script>
</body>
</html>